package com.lh_travel.lohas.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Poi;
import com.amap.api.navi.AmapNaviPage;
import com.amap.api.navi.AmapNaviParams;
import com.amap.api.navi.AmapNaviType;
import com.amap.api.navi.INaviInfoCallback;
import com.amap.api.navi.model.AMapNaviLocation;
import com.lh_travel.lohas.R;
import com.lh_travel.lohas.activity.webview.WebViewActivity;
import com.lh_travel.lohas.adapter.BaseRecyclerAdapter;
import com.lh_travel.lohas.adapter.BaseRecyclerHolder;
import com.lh_travel.lohas.base.BaseFragment;
import com.lh_travel.lohas.domain.CommentIntentBean;
import com.lh_travel.lohas.domain.HotelDetail;
import com.lh_travel.lohas.interfaces.PermissionListener;
import com.lh_travel.lohas.interfaces.VPSet;
import com.lh_travel.lohas.utils.MapUtil;
import com.lh_travel.lohas.utils.Preferences;
import com.squareup.picasso.Picasso;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import cn.carbs.android.expandabletextview.library.ExpandableTextView;

@SuppressLint("ValidFragment")
public class InfoFragment extends BaseFragment {

    private LinearLayout ll_price;
    private HotelDetail detailBean;
    private ImageView img_map;
    private LinearLayout ll_info;
    private RecyclerView rcy;
    private List<String> device;
    private VPSet vpSet;
    private TextView tv_device;
    private String url;
    private String checkIn;
    private String checkOut;

    public InfoFragment() {
    }

    public InfoFragment(HotelDetail response, VPSet vpSet, String checkIn, String checkOut) {
        super();
        this.detailBean = response;
        this.vpSet = vpSet;
        this.checkIn = checkIn;
        this.checkOut = checkOut;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_detail_info;
    }

    @Override
    protected void initView() {
        ll_price = findView(R.id.ll_price);
        img_map = findView(R.id.img_map);
        ll_info = findView(R.id.ll_info);
        rcy = findView(R.id.rcy);
        tv_device = findView(R.id.tv_device);

        initPrice();

        //设置地图
        Picasso.with(context).load(detailBean.map_image).
                placeholder(R.drawable.default120).
                fit().centerCrop().into(img_map);

        img_map.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到导航界面

                double[] latlng = MapUtil.bd09_To_Gcj02(Double.valueOf(detailBean.lat), Double.valueOf(detailBean.lng));

                double start_lat = Double.parseDouble(mapp.getPreference(Preferences.LOCAL.LAT));
                double start_lng = Double.parseDouble(mapp.getPreference(Preferences.LOCAL.LNG));

                LatLng mStartLatlng = new LatLng(start_lat, start_lng);
                LatLng mEndLatlng = new LatLng(latlng[0], latlng[1]);

                AmapNaviParams params = new AmapNaviParams(new Poi("当前位置", mStartLatlng, ""), null, new Poi("终点", mEndLatlng, ""), AmapNaviType.DRIVER);
                params.setUseInnerVoice(true);
                AmapNaviPage.getInstance().showRouteActivity(context, params, iNaviInfoCallback);

            }
        });

        initInfo();

        initDevice();
    }

    private void initDevice() {

        if (detailBean.device != null && detailBean.device.size() > 0) {

            device = new ArrayList<>();

            tv_device.setVisibility(View.VISIBLE);

            if (detailBean.device.size() > 10) {
                for (int i = 0; i < 10; i++) {
                    device.add(detailBean.device.get(i));
                }

            } else {
                device.addAll(detailBean.device);
            }

            rcy.setLayoutManager(new GridLayoutManager(context, 2));

            BaseRecyclerAdapter<String> deviceAdapter = new BaseRecyclerAdapter<String>(context, device, R.layout.item_price_device) {
                @Override
                public void convert(BaseRecyclerHolder holder, String item, int position, boolean isScrolling) {

                    holder.setText(R.id.tv_title, item);

                }
            };

            rcy.setAdapter(deviceAdapter);
        }

    }

    private void initInfo() {

        initTran();
        initTel();
        initAddress();

    }

    private void initAddress() {
        if (detailBean.address.length() > 0) {
            View inflate = View.inflate(context, R.layout.item_price_address, null);

            TextView tv_content = inflate.findViewById(R.id.tv_content);

            RelativeLayout rl_main = inflate.findViewById(R.id.rl_main);

            tv_content.setText(detailBean.address);

            rl_main.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    double[] latlng = MapUtil.bd09_To_Gcj02(Double.valueOf(detailBean.lat), Double.valueOf(detailBean.lng));

                    double start_lat = Double.parseDouble(mapp.getPreference(Preferences.LOCAL.LAT));
                    double start_lng = Double.parseDouble(mapp.getPreference(Preferences.LOCAL.LNG));

                    LatLng mStartLatlng = new LatLng(start_lat, start_lng);
                    LatLng mEndLatlng = new LatLng(latlng[0], latlng[1]);

                    AmapNaviParams params = new AmapNaviParams(new Poi("当前位置", mStartLatlng, ""), null, new Poi("终点", mEndLatlng, ""), AmapNaviType.DRIVER);
                    params.setUseInnerVoice(true);
                    AmapNaviPage.getInstance().showRouteActivity(context, params, iNaviInfoCallback);
                }
            });

            ll_info.addView(inflate);

        }
    }

    private void initTel() {
        if (detailBean.phone.length() > 0) {
            View inflate = View.inflate(context, R.layout.item_price_info, null);

            TextView tv_title = inflate.findViewById(R.id.tv_title);

            TextView tv_content = inflate.findViewById(R.id.tv_content);

            tv_title.setText(context.getString(R.string.tel));

            tv_content.setText(detailBean.phone);

            inflate.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showMyDialog("提示", "拨打乐活客服电话?", "确定", "取消", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                            callPhone(detailBean.phone);

                            dialog.dismiss();

                        }
                    });
                }
            });

            ll_info.addView(inflate);
        }
    }

    private void initTran() {

        if (detailBean.desc.length() > 0) {
            View inflate = View.inflate(context, R.layout.item_price_info, null);

            TextView tv_title = inflate.findViewById(R.id.tv_title);

            ExpandableTextView tv_content = inflate.findViewById(R.id.tv_content);

            tv_title.setText(context.getString(R.string.introduction));

            DisplayMetrics dm = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);

            tv_content.setText(detailBean.desc);

            ll_info.addView(inflate);
        }

    }

    public void callPhone(String phoneNum) {
        Intent intent = new Intent(Intent.ACTION_CALL);
        Uri data = Uri.parse("tel:" + phoneNum);
        intent.setData(data);
        startActivity(intent);
    }

    private void initPrice() {

        if (detailBean.rate > 0.1) {
            View view = View.inflate(context, R.layout.item_price_hint, null);

            TextView tv_price_hint = view.findViewById(R.id.tv_price_hint);

            double value = new BigDecimal(detailBean.rate).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

            int value2 = (int) (value * 100);

            tv_price_hint.setText(context.getString(R.string.price_hint1) + value2 + context.getString(R.string.price_hint2));

            ll_price.addView(view);
        }

        int count = (detailBean.shop.size() > 2 ? 2 : detailBean.shop.size());

        for (int i = 0; i < count; i++) {

            View inflate = View.inflate(context, R.layout.item_detail_price, null);

            ImageView img = inflate.findViewById(R.id.img);
            TextView tv_info = inflate.findViewById(R.id.tv_info);
            TextView tv_price = inflate.findViewById(R.id.tv_price);
            TextView tv_content = inflate.findViewById(R.id.tv_content);
            RelativeLayout main = inflate.findViewById(R.id.main);

            Picasso.with(context).load(detailBean.shop.get(i).picture).
                    placeholder(R.drawable.default120).
                    fit().centerCrop().
                    into(img);

            tv_info.setText(detailBean.shop.get(i).theme);
            tv_price.setText("¥" + detailBean.shop.get(i).third_price);
            tv_content.setText(detailBean.shop.get(i).breakfast + "  " + detailBean.shop.get(i).name);

            final CommentIntentBean bean = new CommentIntentBean();

            bean.keyWord = detailBean.title;
            bean.checkIn = checkIn.substring(5);
            bean.checkOut = checkOut.substring(5);
            bean.url = detailBean.shop.get(i).third_url;
            bean.belong = detailBean.shop.get(i).title;

            main.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Intent intent = new Intent(context, WebViewActivity.class);
                    intent.putExtra("detailBean", bean);
                    intent.putExtra("shop", detailBean.shop);
                    startActivity(intent);

                    getActivity().overridePendingTransition(R.anim.act_bottom_in, R.anim.act_top_out);
                }
            });
            ll_price.addView(inflate);
        }

        if (detailBean.shop.size() > 2) {

            View foot = View.inflate(context, R.layout.item_price_check, null);

            TextView tv_check = foot.findViewById(R.id.tv_check);

            tv_check.setText(context.getString(R.string.detail_check) + detailBean.shop.size() + "个");

            tv_check.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    vpSet.setVPIndex(3);
                }
            });

            ll_price.addView(foot);
        }


    }


    @Override
    protected void initData() {

    }

    private INaviInfoCallback iNaviInfoCallback = new INaviInfoCallback() {
        @Override
        public void onInitNaviFailure() {

        }

        @Override
        public void onGetNavigationText(String s) {

        }

        @Override
        public void onLocationChange(AMapNaviLocation aMapNaviLocation) {

        }

        @Override
        public void onArriveDestination(boolean b) {

        }

        @Override
        public void onStartNavi(int i) {

        }

        @Override
        public void onCalculateRouteSuccess(int[] ints) {

        }

        @Override
        public void onCalculateRouteFailure(int i) {

        }

        @Override
        public void onStopSpeaking() {

        }

        @Override
        public void onReCalculateRoute(int i) {

        }

        @Override
        public void onExitPage(int i) {

        }

        @Override
        public void onStrategyChanged(int i) {

        }

        @Override
        public View getCustomNaviBottomView() {
            return null;
        }

        @Override
        public View getCustomNaviView() {
            return null;
        }

        @Override
        public void onArrivedWayPoint(int i) {

        }
    };
}
